//样式，评论
var showSafeComment;
require(['grid', 'topBar', 'date', 'highcharts'], function () {
    //提交到后台参数
    var postData;
    $('#top').topBar({
        left: [
            {
                type: 'filter',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name: 'reportType',
                        targetId: 'beginTime',
                        type: 'reportDateSelect'
                    }, {
                        text: '查询时间',
                        id: 'beginTime',
                        name: 'beginTime',
                        type: 'text',
                        width: 250
                    }
                ]
            }
        ],
        right: [{
            text: '导出',
            type: 'export-btn',
            authority: '',
            item: [{
                text: 'Excel导出',
                click: function () {
                    exportFile('Excel');
                }
            }, {
                text: 'PDF导出',
                click: function () {
                    exportFile('Pdf');
                }
            }]
        }]
    });
    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") < 1) {
            $.messager.alert("提示", "请先查询数据。", "info");
            return;
        }
        if (getPostData()) {
            var url = "/safeBehaviour/exportSafeBehaviour" + suffix + ".htm";
            $.exportFile(url, postData);
        }
    }

    $('#grid').parent().height($(window).height() - 174);

    /*
     *
     驾驶员考核模块：
     安全行为、节能降耗、驾驶员情绪
     三类评语接口统一调用DriverCommentService
     */
    showSafeComment = function (rowObject) {
        if (rowObject.allRank == '-') {
            $("#showSafeMsg").css("display", "none");
        } else {
            var param = $('#top').parent().getValues();
            param.drvName = rowObject.drvName;
            param.lineName = rowObject.lineName;
            param.organName = rowObject.organName;
            param.bhvCount1 = rowObject.summary1;
            param.bhvCount2 = rowObject.summary2;
            param.bhvCount3 = rowObject.overSpdingCount;
            param.bhvCount4 = "0";
            param.bhvCount5 = rowObject.summary5;
            param.bhvCount6 = rowObject.summary6;
            param.bhvCount7 = rowObject.summary7;
            param.bhvCount8 = rowObject.summary8;
            param.bhvCount9 = rowObject.summary9;
            param.compareValue = rowObject.distanScore;
            param.lineId = rowObject.lineId;
            $.ajaxExt({
                data: param,
                url: "/safeBehaviour/getSafeBehaviourComments.htm"
            }).done(function (result) {
                if (result[1] == "优秀" || result[1] == "良好") {
                    $("#safeTitle").html("<font style = 'color:green;'>" + result[1] + "</font>");
                } else if (result[1] == "合格" || result[1] == "基本合格") {
                    $("#safeTitle").html("<font style = 'color:orange;'>" + result[1] + "</font>");
                } else {
                    $("#safeTitle").html("<font style = 'color:red;'>" + result[1] + "</font>");
                }
                $("#safeComments").html("<b>评语:</b>" + result[0]);

                $("#SafeDrvName").html(rowObject.drvName);
                $("#safeScore").html(rowObject.distanScore);
                $("#totalCount").html(rowObject.bhvAll);
            });

            initSafeCharts(rowObject);

            $("#showSafeMsg").css("display", "block");
        }
    }

    //图示
    function initSafeCharts(rowObject) {
        $("#safeBehaviourCharts").highcharts({
            chart: {
                type: 'bar'
            },
            title: {
                text: ''
            },
            credits: {
                enabled: false
            },
            xAxis: {
                categories: ["异常开门(次)", "异常关门(次)", "空挡滑行(次)", "倒车加速(次)", "ABS(次)", "夜间行车(次)", "起步急加(次)", "熄火滑行(次)", "急加速(次)", "急减速(次)", "急刹(次)", "超速(次)"]
            },
            yAxis: {
                lineWidth: 1,
                title: {
                    text: ''
                }
            },
            tooltip: {
                pointFormat: '{point.y} 次</b>'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [
                {
                    name: '次数',
                    data: [parseInt(rowObject.summary1), parseInt(rowObject.summary2), parseInt(rowObject.summary5), parseInt(rowObject.summary6), parseInt(rowObject.summary7), parseInt(rowObject.summary8), parseInt(rowObject.summary9), parseInt(Object.summary10), parseInt(Object.summary11), parseInt(Object.summary12), parseInt(Object.summary13), parseInt(rowObject.overSpdingCount)]
                }
            ]
        });
    }

    $('#grid').grid({
        url: "/safeBehaviour/searchSafeBehaviour.htm",
        multiselect: false,
        colModel: [{name: "lineId", label: "lineId", hidden: true},
            {name: "allRank", label: "排名", align: "right", width: 40},
            {name: "distanScore", label: "得分", align: "right", formatter: "currency", width: 40},
            {name: "drvName", label: "驾驶员", width: 80},
            {name: "lineName", label: "营运线路", width: 80},
            {name: "organName", label: "所属机构", width: 80},
            {name: "lastingTime", label: "运行时长", align: "right", width: 80},
            {
                name: "totalMile", label: "运行里程(KM)", align: "right", width: 120,
                formatter: function (cellvalue, options, rowObject) {
                    if (rowObject.totalMile) {
                        return rowObject.totalMile.toFixed(2);
                    } else {
                        return '-';
                    }
                }
            },
            {name: "bhvAll", label: "行为总数(次)", align: "right", width: 120},
            {
                name: "bhvPerMile", label: "行为次数(次/千公里)", align: "right", width: 200,
                formatter: function (cellvalue, options, rowObject) {
                    if (rowObject.bhvPerMile) {
                        return rowObject.bhvPerMile.toFixed(2);
                    } else {
                        return '-';
                    }
                }
            },
            {name: "summary1", label: "异常开门(次)", align: "right", width: 120},
            {name: "summary2", label: "异常关门(次)", align: "right", width: 120},
            {name: "summary5", label: "空挡滑行(次)", align: "right", width: 120},
            {name: "summary6", label: "倒车加速(次)", align: "right", width: 120},
            {name: "summary7", label: "ABS(次)", align: "right", width: 100},
            {name: "summary8", label: "夜间行车(次)", align: "right", width: 100},
            {name: "summary9", label: "起步急加(次)", align: "right", width: 100},
            {name: "summary10", label: "熄火滑行(次)", align: "right", width: 100},
            {name: "summary11", label: "急加速(次)", align: "right", width: 100},
            {name: "summary12", label: "急减速(次)", align: "right", width: 100},
            {name: "summary13", label: "急刹(次)", align: "right", width: 100},
            {name: "overSpdingCount", label: "超速(次)", align: "right", width: 100}
        ],
        onSelectRow: function (ids) {
            var data = $('#grid').getRowData(ids);
            showSafeComment(data);
        }
    });


    function search() {
        if (getPostData()) {
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
        }
    }

    function getPostData() {
        postData = $('#top').parent().getValues();
        if(!postData.beginTime){
            $.messager.alert("提示", "请先选择查询时间。", "info");
            return false;
        }
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if (nodes.LINE) {
            postData.orgIds = nodes.LINE;
            postData.type = "LINE";
            return true;
        }
        $.messager.alert("提示", "至少请选择一条线路。", "info");
        return false;
    }

})
